package com.baselib.libnetwork.net.libnetworkcomponent;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.HashMap;

/**
 * Created by lixuce on 2018/5/3.
 * 如果应用里面有一些公共参数的情况下   可以都放到这个类里面
 *
 */

public class RequestCommonParams {

    private static final RequestCommonParams instance = new RequestCommonParams();

    private final HashMap<String, String> map = new HashMap<>();
    private final String KEY_APP_IMEI = "_imei";
    private final String VALUE_APP = "1";
    private final String VALUE_APP_TYPE = "200";
    private Context applicationContext;
    private String valueImei;


    private RequestCommonParams() {

    }

    public static RequestCommonParams getInstance() {
        return instance;
    }

    public void onInit(Context context) {
        map.clear();
        applicationContext = context;
        map.put("_app", VALUE_APP);
        map.put("_at", "w");
        map.put("_c", "w");
        map.put("_net", getAppNet());
        map.put("_t", VALUE_APP_TYPE);
        map.put("_v", getAppVersion());
        map.put("_s_n", getAppMode());
        map.put("_s_v", getAppCode());
        valueImei = context.getSharedPreferences("RequestCommonParams", Context.MODE_PRIVATE).getString(KEY_APP_IMEI, "");
        map.put("_imei", valueImei);
    }

    private String getAppNet() {
// String type = "unknown";
        int state = NetState.UNKOWN.state;
        String type = NetState.UNKOWN.type;
        ConnectivityManager cm = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo info = cm.getActiveNetworkInfo();

        if (info == null) {
            // type = "unknown";
            state = NetState.UNKOWN.state;

        } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            // type = "wifi";
            state = NetState.WIFI.state;
        } else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            int subType = info.getSubtype();
            if (subType == TelephonyManager.NETWORK_TYPE_CDMA || subType == TelephonyManager.NETWORK_TYPE_GPRS
                    || subType == TelephonyManager.NETWORK_TYPE_EDGE) {
                // type = "2g";
                state = NetState.CDMA.state;
            } else if (subType == TelephonyManager.NETWORK_TYPE_UMTS || subType == TelephonyManager.NETWORK_TYPE_HSDPA
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_A
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_0
                    || subType == TelephonyManager.NETWORK_TYPE_EVDO_B) {
                // type = "3g";
                state = NetState.UMTS.state;
            } else {// LTE是3g到4g的过渡，是3.9G的全球标准 if (subType ==
                // TelephonyManager.NETWORK_TYPE_LTE)
                // type = "4g";
                state = NetState.LTE.state;
            }
        }
        switch (state) {
            case 1:
                type = NetState.WIFI.type;
                break;
            case 2:
                type = NetState.CDMA.type;
                break;
            case 3:
                type = NetState.UMTS.type;
                break;
            case 4:
                type = NetState.LTE.type;
                break;
            case 5:
            default:
                type = NetState.UNKOWN.type;
                break;
        }
        return type;
    }



    private String getAppMode() {
        return Build.MODEL;
    }


    public HashMap<String, String> getCommonParams() {
        return map;
    }


    public void updateToken(String token) {
        if (TextUtils.isEmpty(token)) {
            return;
        }
        map.put("_token", token);
    }

    public void updateImei(String imei) {
        if (instance==null) {
            return;
        }
        if (TextUtils.isEmpty(imei)) {
            return;
        }
        map.put(KEY_APP_IMEI, imei);
        //保存到本地
        applicationContext.getSharedPreferences("RequestCommonParams", Context.MODE_PRIVATE).edit().putString(KEY_APP_IMEI, imei).apply();
    }


    private String getAppVersion() {

        String appVersion = "";
        try {
            PackageManager pManager = applicationContext.getPackageManager();
            PackageInfo packageInfo = pManager.getPackageInfo(applicationContext.getPackageName(), 0);
            appVersion = String.valueOf(packageInfo.versionCode);
        } catch (Exception e) {
        }
        return appVersion;
    }

    public String getImei() {
        return valueImei;
    }

    public String getAppCode() {
        return Build.VERSION.RELEASE;
    }


    enum NetState {
        WIFI("wifi", 1), CDMA("2G", 2), UMTS("3G", 3), LTE("4G", 4), UNKOWN("unkonw", 5);
        private int state;
        private String type;

        NetState(String type, int state) {
            this.state = state;
            this.type = type;
        }
    }
}
